package com.estate.struts.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.estate.business.DocumentService;
import com.estate.business.EfileService;
import com.estate.utils.FileUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.DocumentVo;
import com.estate.vo.impl.EfileVo;
import com.estate.vo.impl.UserInfoVo;

public class EfileAction extends EstateSupport {
	
	private int id;
	private String documentId;
	private String fileSaveName;
	private Date uploadDate;
	private UserInfoVo uploadUser;
	private String filePath;
	private String fileCompany;
	private int secretRate;
	private int urgentRate;
	private String fileRealName;
	private String saveFile;
	private String saveName;
	
	private String Filedata;
	private String FiledataFileName;
	
	private String method;
	private EfileService efileService;
	private DocumentService documentService;
	private String multiType;
	private String multiDelete;
	private String delStr;
	private String deleteId;
	
	public String execute() throws Exception{
		String page="";
		System.out.println(method);
		if("list".equals(method)){
			List<Vo> voList=efileService.findAllEfile();
			for(int i=0;i<voList.size();i++){
				int id=((EfileVo)voList.get(i)).getDocumentId();
				((EfileVo)voList.get(i)).setDocumentName(((DocumentVo)documentService.findByPK(id)).getDocumentName());
			}
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			this.getSession().setAttribute("efileList", voList);
			page="list";
		} else if("delete".equals(method)){
			if ("true".equals(multiDelete)) {
				System.out.println("!!!!!!!!!"+delStr);
				if (delStr != null && !"".equals(delStr) && !",".equals(delStr)) {
					String[] delArray = delStr.split(",");
					for (int i = 0; i < delArray.length; i++) {
						efileService.deleteByPK(Integer
								.parseInt(delArray[i]));
					}
				}
			} else {
				efileService.deleteByPK(Integer.parseInt(this.getDeleteId()));
			}
			page="refresh";
		}else if("upload".equals(method)){
			File directory=new File(FileUtil.COMPANYSTUFF_FILE);
			saveName=FileUtil.getReplaceFileName(this.getFiledataFileName());
			saveFile=directory+File.separator+saveName;
			this.getSession().setAttribute("saveName", saveName);
			this.getSession().setAttribute("saveFile", saveFile);
			this.getSession().setAttribute("realName", this.getFiledataFileName());
			FileInputStream fis=new FileInputStream(this.getFiledata());
			
			if(!directory.exists()){
				directory.mkdir();
			}
			
			FileOutputStream fos=new FileOutputStream(saveFile);
			byte[] buffer=new byte[1024];
			int length;
			while((length=fis.read(buffer))>0){
				fos.write(buffer,0,length);
			}
			fis.close();
			fos.close();
			System.out.println("out load");
			page="upload";
		} else if("write".equals(method)){
			EfileVo vo=new EfileVo();
			vo.setSecretRate(secretRate);
			vo.setUrgentRate(urgentRate);
			vo.setFilePath(this.getSession().getAttribute("saveFile").toString());
			vo.setDocumentId(Integer.parseInt(documentId));
			vo.setFileCompany(fileCompany);
			vo.setFileRealName(this.getSession().getAttribute("realName").toString());
			vo.setFileSaveName(this.getSession().getAttribute("saveName").toString());
			vo.setUploadUser(this.getSessionUser());
			vo.setUploadDate(uploadDate);
			efileService.addEfile(vo);
			this.getRequest().setAttribute("isNameVisible", new Boolean(true));
			this.getRequest().setAttribute("fileName", this.getFiledataFileName());
			page="upload";
		} else if("add".equals(method)){
			HashMap<Integer,String> documentMap=documentService.findAllIdAndName();
			System.out.println(documentMap);
			this.getSession().setAttribute("documentSelect", documentMap);
			page="add";
		} else if("newfile".equals(method)){
			EfileVo vo=new EfileVo();
			vo.setSecretRate(secretRate);
			vo.setUrgentRate(urgentRate);
			vo.setFilePath(this.getSession().getAttribute("saveFile").toString());
			vo.setDocumentId(Integer.parseInt(documentId));
			vo.setFileCompany(fileCompany);
			vo.setFileRealName(this.getSession().getAttribute("realName").toString());
			vo.setFileSaveName(this.getSession().getAttribute("saveName").toString());
			vo.setUploadUser(this.getSessionUser());
			vo.setUploadDate(uploadDate);
			efileService.addEfile(vo);
			this.getRequest().setAttribute("isNameVisible", new Boolean(true));
			this.getRequest().setAttribute("fileName", this.getFiledataFileName());
			page="newfile";
		}
		System.out.println(page);
		return page;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getDocumentId() {
		return documentId;
	}
	public void setDocumentId(String documentId) {
		this.documentId = documentId;
	}
	public String getFileSaveName() {
		return fileSaveName;
	}
	public void setFileSaveName(String fileSaveName) {
		this.fileSaveName = fileSaveName;
	}
	public Date getUploadDate() {
		return uploadDate;
	}
	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}
	
	public UserInfoVo getUploadUser() {
		return uploadUser;
	}
	public void setUploadUser(UserInfoVo uploadUser) {
		this.uploadUser = uploadUser;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public String getFileCompany() {
		return fileCompany;
	}
	public void setFileCompany(String fileCompany) {
		this.fileCompany = fileCompany;
	}
	public int getSecretRate() {
		return secretRate;
	}
	public void setSecretRate(int secretRate) {
		this.secretRate = secretRate;
	}
	public int getUrgentRate() {
		return urgentRate;
	}
	public void setUrgentRate(int urgentRate) {
		this.urgentRate = urgentRate;
	}
	public String getFileRealName() {
		return fileRealName;
	}
	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMethod() {
		return method;
	}
	public void setEfileService(EfileService efileService) {
		this.efileService = efileService;
	}
	public EfileService getEfileService() {
		return efileService;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setFiledata(String filedata) {
		Filedata = filedata;
	}

	public String getFiledata() {
		return Filedata;
	}

	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public String getFiledataFileName() {
		return FiledataFileName;
	}

	public void setSaveFile(String saveFile) {
		this.saveFile = saveFile;
	}

	public String getSaveFile() {
		return saveFile;
	}

	public void setSaveName(String saveName) {
		this.saveName = saveName;
	}

	public String getSaveName() {
		return saveName;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	public DocumentService getDocumentService() {
		return documentService;
	}
	
}
